%{

#include "parser.h"
#include <cstdlib>
#include <string>
#include <fstream>

std::string parsedString;
std::ifstream inputFile;
unsigned line = 1;

#define yyterminate() return TOKEN_END;

#define YY_INPUT(buf, result, max_size) \
	{ \
	if (inputFile.good()) { \
		inputFile >> buf[0]; \
		result = 1; \
	} \
	else result = YY_NULL; \
	}

%}

%option caseless

%x STR
%x STRNL
%x ESCAPE

DIGIT [0-9]
LINESPACE [ \t\f]
NEWLINE \r\n|\n|\r

%%

command			return TOKEN_COMMAND;
topic			return TOKEN_TOPIC;
name			return TOKEN_NAME;
synopsis		return TOKEN_SYNOPSIS;
description		return TOKEN_DESCRIPTION;
notes			return TOKEN_NOTES;
seealso			return TOKEN_SEEALSO;

<STR>{LINESPACE} { parsedString.push_back(yytext[0]); }
\"				{ BEGIN(STR); parsedString.clear(); }
<STR>\"			{ BEGIN(INITIAL); yylval.strVal = parsedString.c_str(); return TOKEN_STRING; }
<STR>\\			{ BEGIN(ESCAPE); }
<STR>{NEWLINE}  { BEGIN(STRNL); parsedString.append("\\n"); line++; }
<STRNL>{LINESPACE}+ /* gobble up */
<STRNL>{NEWLINE} { line++; }
<STRNL>.		{ yyless(0); BEGIN(STR); }
<ESCAPE>.		{ BEGIN(STR); parsedString.push_back('\\'); parsedString.push_back(yytext[0]); }
<STR>.			{ parsedString.push_back(yytext[0]); }

{LINESPACE}+   /* gobble up */
{NEWLINE}		{ line++; }
{DIGIT}+		{ yylval.intVal = atoi(yytext); if (yylval.intVal < 1) yylval.intVal = 1; if (yylval.intVal > 1000) yylval.intVal = 1000; return TOKEN_NUMBER; }

.				{ return TOKEN_UNKNOWN; }

%%

int yywrap (void) {return 1;}

